package com.woniuxy.mqdemo;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.data.redis.connection.RedisConnection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: 马宇航
 * @Todo: 生产者
 * @DateTime: 25/08/29/星期五 11:27
 * @Component: 成都蜗牛学苑
 **/
public class ProductMessage {
    private static final String QUEUE_NAME = "java115_demo1";
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.第一步，实例化工厂，后期就交给springboot的yml和自动配置类来实现
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.115.5");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //虚拟环境
        connectionFactory.setVirtualHost("java115");
        //2.第二步，创建连接，连接创建信道 channel
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        //3.第三步，声明一个队列，一旦声明，mq的web端就能看见
        channel.queueDeclare(QUEUE_NAME,true,false,false,null);
        //4.第四步，发送消息 交换机参数，使用的是默认交换机（空串交换机） getBytes 字节流
        channel.basicPublish("",QUEUE_NAME,null,"hello java115期".getBytes());
        //5.第五步，关闭通道和连接
        channel.close();
        connection.close();
    }
}
